#include <cstdio>

__int64 mpow( __int64 p, __int64 q, __int64 r )
{
    __int64 pos = 1, ret = 1;
    while ( pos <= q ) pos <<= 1;
    pos >>= 1;
    while ( pos )
    {
        ret = ( ret * ret ) % r;
        if ( pos & q )
            ret = ( ret * p ) % r;
        pos >>= 1;
    }
    return ret;
}

int main( )
{
    __int64 n;
    while ( scanf("%I64d", &n) && n )
    {
        printf("%I64d\n", ( mpow( 2, n - 1, n ) + 1 ) % n);
    }
    return 0;
}
